cmake_minimum_required(VERSION 3.0)

project(efso LANGUAGES Fortran)

if (CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
  set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -ffree-line-length-none")
  if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -O0 -fcheck=all -g -fbacktrace")
  else ()
    set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Ofast")
  endif ()
elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "Intel")
	if (CMAKE_BUILD_TYPE STREQUAL "Debug")
		set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -no-wrap-margin -O0 -check all -traceback")
	else ()
		set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -no-wrap-margin -O3")
	endif ()
endif ()

if (DEFINED ENV{LAPACK_ROOT})
  message(STATUS "Use LAPACK at $ENV{LAPACK_ROOT}.")
  include_directories($ENV{LAPACK_ROOT}/include)
  link_directories($ENV{LAPACK_ROOT}/lib)
  link_directories($ENV{LAPACK_ROOT}/lib64)
  set(EXTERNAL_LIBS ${EXTERNAL_LIBS} blas lapack)
elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "Intel")
  set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -mkl")

  if (DEFINED ENV{MKLROOT})
    set(HAS_MKL ON)
    include_directories("$ENV{MKLROOT}/include")
    link_directories("$ENV{MKLROOT}/lib/intel64")
  elseif (DEFINED ENV{MKL_INC} AND DEFINED ENV{MKL_LIB})
    set(HAS_MKL ON)
    include_directories("$ENV{MKL_INC}")
    link_directories("$ENV{MKL_LIB}")
  else ()
    message (FATAL_ERROR "Unable to find MKL library!")
  endif ()

  if (HAS_MKL)
    message (STATUS "Use MKL library.")
    add_definitions(-DHAS_MKL)
    set(EXTERNAL_LIBS ${EXTERNAL_LIBS} mkl_blas95_ilp64 mkl_lapack95_ilp64)
  endif ()
endif ()

if (DEFINED ENV{ECCODES_ROOT})
  set(ECCODES_INCLUDE_DIRS $ENV{ECCODES_ROOT}/include)
  set(ECCODES_INCLUDE_DIRS $ENV{ECCODES_ROOT}/include/x86_64-linux-gnu)
  set(ECCODES_INCLUDE_DIRS $ENV{ECCODES_ROOT}/lib/x86_64-linux-gnu/fortran/gfortran-mod-15)
  set(ECCODES_LIBRARY_DIRS $ENV{ECCODES_ROOT}/lib)
else ()
  find_package(ECCODES REQUIRED FORTRAN)
endif ()

include_directories(${ECCODES_INCLUDE_DIRS})
link_directories(${ECCODES_LIBRARY_DIRS})
set(EXTERNAL_LIBS ${EXTERNAL_LIBS} eccodes eccodes_f90)

add_subdirectory(lib/string)
add_subdirectory(lib/datetime)
add_subdirectory(lib/flogger)
add_subdirectory(lib/container)
add_subdirectory(lib/fiona)
add_subdirectory(lib/kdtree)
add_subdirectory(lib/data-translators)

include_directories("${DATA_TRANSLATORS_INC}")
link_directories("${DATA_TRANSLATORS_LIB}")

set(source_files
  src/const_mod.F90
  src/math_mod.F90
  src/sphere_geometry_mod.F90
  src/namelist_mod.F90
  src/latlon_mesh_mod.F90
  src/latlon_process_mod.F90
  src/latlon_halo_mod.F90
  src/latlon_parallel_mod.F90
  src/latlon_data_mod.F90
  src/latlon_interp_mod.F90
  src/vert_interp_mod.F90
  src/grapes_reader_mod.F90
  src/era5_reader_mod.F90
  src/era5_regrid_mod.F90
  src/obs_synop_mod.F90
  src/obs_raob_mod.F90
  src/obs_amdar_mod.F90
  src/obs_profiler_mod.F90
  src/obs_data_mod.F90
  src/obs_err_mod.F90
  src/obs_operator_types_mod.F90
  src/obs_operator_interp_mod.F90
  src/obs_operators_mod.F90
  src/efso_utils_mod.F90
  src/efso_mod.F90
)

add_library(efso ${source_files})
target_link_libraries(efso
  flogger
  fortran_string
  fortran_datetime
  fortran_container
  fiona
  fortran_kdtree
  data_translators
  ${EXTERNAL_LIBS}
 )

add_executable(efso_driver.exe src/efso_driver.F90)
target_link_libraries(efso_driver.exe efso)
